namespace PMS.Common
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;

    public class Utils
    {
        public static string DelLastChar(string str, string strchar)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            if ((str.LastIndexOf(strchar) >= 0) && (str.LastIndexOf(strchar) == (str.Length - 1)))
            {
                return str.Substring(0, str.LastIndexOf(strchar));
            }
            return str;
        }

        public static string DelLastComma(string str)
        {
            return str.Substring(0, str.LastIndexOf(","));
        }

        public static string Filter(string sInput)
        {
            if ((sInput == null) || (sInput == ""))
            {
                return null;
            }
            string input = sInput.ToLower();
            string str2 = sInput;
            string str = "*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
            if (Regex.Match(input, Regex.Escape(str), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
            {
                throw new Exception("字符串中含有非法字符!");
            }
            return str2.Replace("'", "''");
        }

        public static string GetArrayStr(List<string> list, string speater)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == (list.Count - 1))
                {
                    builder.Append(list[i]);
                }
                else
                {
                    builder.Append(list[i]);
                    builder.Append(speater);
                }
            }
            return builder.ToString();
        }

        public static string GetRamCode()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssffff");
        }

        public static string[] GetStrArray(string str)
        {
            return str.Split(new char[0x2c]);
        }

        public static string Htmls(string Input)
        {
            if ((Input != string.Empty) && (Input != null))
            {
                return Input.ToLower().Replace("<script", "&lt;script").Replace("script>", "script&gt;").Replace("<%", "&lt;%").Replace("%>", "%&gt;").Replace("<$", "&lt;$").Replace("$>", "$&gt;");
            }
            return string.Empty;
        }

        public static bool IsDouble(object expression)
        {
            return ((expression != null) && Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$"));
        }

        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

        public static bool IsNumeric(object expression)
        {
            return ((expression != null) && IsNumeric(expression.ToString()));
        }

        public static bool IsNumeric(string expression)
        {
            if (expression != null)
            {
                string input = expression;
                if ((((input.Length > 0) && (input.Length <= 11)) && Regex.IsMatch(input, "^[-]?[0-9]*[.]?[0-9]*$")) && (((input.Length < 10) || ((input.Length == 10) && (input[0] == '1'))) || (((input.Length == 11) && (input[0] == '-')) && (input[1] == '1'))))
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsSafeSqlString(string str)
        {
            return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
        }

        public static DateTime ObjectToDateTime(object obj)
        {
            return StrToDateTime(obj.ToString());
        }

        public static DateTime ObjectToDateTime(object obj, DateTime defValue)
        {
            return StrToDateTime(obj.ToString(), defValue);
        }

        public static float ObjToFloat(object expression, float defValue)
        {
            if (expression != null)
            {
                return StrToFloat(expression.ToString(), defValue);
            }
            return defValue;
        }

        public static int ObjToInt(object expression, int defValue)
        {
            if (expression != null)
            {
                return StrToInt(expression.ToString(), defValue);
            }
            return defValue;
        }

        public static string[] SplitString(string strContent, string strSplit)
        {
            if (!string.IsNullOrEmpty(strContent))
            {
                if (strContent.IndexOf(strSplit) < 0)
                {
                    return new string[] { strContent };
                }
                return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
            }
            return new string[0];
        }

        public static string[] SplitString(string strContent, string strSplit, int count)
        {
            string[] strArray = new string[count];
            string[] strArray2 = SplitString(strContent, strSplit);
            for (int i = 0; i < count; i++)
            {
                if (i < strArray2.Length)
                {
                    strArray[i] = strArray2[i];
                }
                else
                {
                    strArray[i] = string.Empty;
                }
            }
            return strArray;
        }

        public static bool SqlFilter(string word, string InText)
        {
            if (InText != null)
            {
                foreach (string str in word.Split(new char[] { '|' }))
                {
                    if ((InText.ToLower().IndexOf(str + " ") > -1) || (InText.ToLower().IndexOf(" " + str) > -1))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static string StringOfChar(int strLong, string str)
        {
            string str2 = "";
            for (int i = 0; i < strLong; i++)
            {
                str2 = str2 + str;
            }
            return str2;
        }

        public static bool StrToBool(object expression, bool defValue)
        {
            if (expression != null)
            {
                return StrToBool(expression, defValue);
            }
            return defValue;
        }

        public static bool StrToBool(string expression, bool defValue)
        {
            if (expression != null)
            {
                if (string.Compare(expression, "true", true) == 0)
                {
                    return true;
                }
                if (string.Compare(expression, "false", true) == 0)
                {
                    return false;
                }
            }
            return defValue;
        }

        public static DateTime StrToDateTime(string str)
        {
            return StrToDateTime(str, DateTime.Now);
        }

        public static DateTime StrToDateTime(string str, DateTime defValue)
        {
            DateTime time;
            if (!string.IsNullOrEmpty(str) && DateTime.TryParse(str, out time))
            {
                return time;
            }
            return defValue;
        }

        public static float StrToFloat(string expression, float defValue)
        {
            if ((expression == null) || (expression.Length > 10))
            {
                return defValue;
            }
            float result = defValue;
            if ((expression != null) && Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
            {
                float.TryParse(expression, out result);
            }
            return result;
        }

        public static int StrToInt(string expression, int defValue)
        {
            int num;
            if (!((!string.IsNullOrEmpty(expression) && (expression.Trim().Length < 11)) && Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$")))
            {
                return defValue;
            }
            if (int.TryParse(expression, out num))
            {
                return num;
            }
            return Convert.ToInt32(StrToFloat(expression, (float) defValue));
        }

        public class VersionInfo
        {
            public int FileBuildPart
            {
                get
                {
                    return 3;
                }
            }

            public int FileMajorPart
            {
                get
                {
                    return 1;
                }
            }

            public int FileMinorPart
            {
                get
                {
                    return 0;
                }
            }

            public string ProductName
            {
                get
                {
                    return "DTcms";
                }
            }

            public int ProductType
            {
                get
                {
                    return 0;
                }
            }
        }
    }
}

